 <?php
  use yii\helpers\Html;
  use yii\helpers\Url;
  use yii\bootstrap\ActiveForm;
  use yii\widgets\LinkPager;

  $this->title = 'viewlist';
 ?>
<style>
  ul,li{margin:0;padding:0;list-style: none;}
  .search_box{display:none;}
  .search_box .zhezhao{position:fixed;top:0;left:0;right:0;bottom:0;background:#000;opacity:0.6;}
  .search_box .search_form{position:absolute;top:10%;width:60%;background:#FFF;left:20%;}
  .search_box .search_form .form-group{margin-right: 0;margin-left:0;}
  .searchsex_box{overflow:hidden;}
  .searchsex_box>span{float:left;margin-top:10px;font-weight:700;}
  .searchsex_box .radio{float:left;margin-left:50px;}
  .search_form .box-footer{text-align:center;}
  .search_form .box-footer button{width:200px;}
  .font-info{margin-left:450px;}
  .trans_user span:hover{text-decoration:underline;}
</style>
<div class="box">
  <div class="box-header">
    <h3 class="box-title"><?= $username ?>的意向产品</h3>
  </div>
  <a href="javascript:void(0)" onclick="back()" class="btn btn-warning" style="float:right">返回</a>
  <!-- /.box-header -->
  <div class="box-body">
    <table id="example1" class="table table-bordered table-striped">
      <thead>
      <tr>
        <th>产品名称</th>
        <th>接待人</th>
        <th>认可度</th>
        <th>疑惑点</th>
        <th>考虑因素</th>
        <th>备注</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <?php foreach ($model as $val) :?>
      <tr>
        <td><?= $val->product->name ?></td>
        <td class="trans_user" intid="<?= $val->id?>"><span style="color:#3399ff;cursor:pointer"><?= $employee[$val->trans_user] ?></span></td>
        <td><?= $val->agreed?></td>
        <td><?= $val->confusion ?></td>
        <td><?= $val->consider?></td>
        <td><?= $val->remark?></td>
        <td><input class="btn btn-danger remove-user" readonly value="移除意向客户" onclick="return remove(<?= $val->id?>)"/></td>
      </tr>
      <?php endforeach;?>
      </tbody>
    </table>
  </div>

  <!-- /.box-body -->
  <?= LinkPager::widget(['pagination' => $pages]); ?>
</div>

<div class="search_box">
  <div class="zhezhao"></div>
  <div class="search_form">
    <?= Html::beginForm(Url::toRoute(['customer/assigntrans', 'uid' => $uid]), 'post', ['id' => 'search-form', 'class'=>'form-horizontal']); ?>
      <div class="box-body">
        <div class="form-group">
          <label for="search_barcode">接待人分配</label>
          <select class="form-control depart">
            <option>选择部门</option>
            <?php foreach($depart as $key=>$val): ?>
              <option value="<?= $key ?>"><?= $val ?></option>
            <?php endforeach;?>
          </select>
          <select class="form-control employees" name="trans_user">
            <option value="">选择接待人</option>
          </select>
          <span class="error-trans" style="color:red"></span>
          <input type="hidden" name="intention" class="hide_intention" value="" />
        </div>
        </div>
      <div class="box-footer">
      <input class='btn btn-primary btn-submit' value="提交" readonly style="width:200px"/>
      <input class="btn btn-warning btn-close" readonly value="关闭" style="width:200px" />
      </div>
    <?= Html::endForm(); ?>
  </div>
</div>

<script type="text/javascript" src="js/jquery.js"></script>
<script>
  function back(){
    location.href = history.go(-1);
  }

  $(".trans_user").click(function(){
    $(".search_box").show();
    var intid = $(this).attr('intid');
    $(".hide_intention").val(intid);
  });

  $(".search_box .zhezhao").click(function(){
    $(this).parent(".search_box").hide();
  });

  $(".btn-close").click(function(){
    $(".search_box").hide();
  });

  $(".depart").change(function(){
    var did = $(this).val();
    var url = "<?= Url::toRoute('customer/getemployee')?>";
    var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
    $.ajax({
      url:url,
      type:'GET',
      dataType:'json',
      data: {
        did:did,
        _csrf:_csrf
      },
      success: function(json){
        var html = "";
        if (json.flag) {
                $.each(json.data, function (n, value) {
                  html += "<option value='"+n+"'>"+value+"</option>";
                });
        } else {
          html = "<option value=''>部门暂无员工</option>";
        }
        $(".employees").html(html);
      },
      error:function(){
        alert('网络忙');
      }
    }); 
  });

  // $(".trans_user").mouseover(function(){
  //   $(this).css({
  //     "background-color":"#3c8dbc",
  //     "color":"white",
  //     // "transition-duration":"1s"
  //   });
  // });

  // $(".trans_user").mouseout(function(){
  //   $(this).css({
  //     "background-color":"#f4f4f4",
  //     "color":"black"
  //   });
  // });

  /*更新接待人*/
  $(".btn-submit").click(function(){
    var trans_user = $(".employees").val();
    if (trans_user == "") {
      $(".error-trans").text('接待人不能为空');
      setTimeout("$('.error-trans').text('')", 3000);
    } else {
      $("#search-form").submit();
    }
  });

  /*移除意向客户*/
  function remove(intid){
      if (confirm('确定移除意向客户吗')) {
        var url = "<?= Url::toRoute('customer/removeintention')?>";
        var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
        $.ajax({
          url:url,
          type:'GET',
          dataType:'json',
          data: {
            intid:intid,
            _csrf:_csrf
          },
          success: function(json){
            if (json.flag) {
              $("table td[intid='"+intid+"']").parent("tr").remove();
            } else {
              alert('操作失败');
            }
          },
          error:function(){
            alert('网络忙');
          }
        });
      }   
  }    
</script>
